package com.ews.onlineexchange.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

/**
 * 待定名单
 * @author Administrator
 *
 */
@Entity
public class UndeterminedRoster extends DataEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<ServiceSupplier>  			suppliers ;								//中介列表
	private	Date 							startDate;								//列入待定名单起始日期
	private Date							endDate;								//列入待定名单截至日期
	private List<Attachment>				evidences = new ArrayList<>();			//证据材料
	private Boolean							auditted;								//是否审核成功
	private User							enteringUser;							//录入人员
	private Date							enteringDate;							//录入日期
	private RejectRecord					rejectRecord;							//驳回原因
	private	String							remark;									//备注
	private User							auditUser;								//审核人员
	private Date							auditDate;								//审核日期
	private boolean							hasSendMsg = false;						//是否已经提醒
	@NotNull
	@ManyToMany
	@JoinTable(name = "UNDETERMINEDROSTER_SUPPLIERS")
	public List<ServiceSupplier> getSuppliers() {
		return suppliers;
	}
	public void setSuppliers(List<ServiceSupplier> suppliers) {
		this.suppliers = suppliers;
	}
	
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	@NotNull
	@OneToMany(cascade = CascadeType.ALL)
	public List<Attachment> getEvidences() {
		return evidences;
	}
	public void setEvidences(List<Attachment> evidences) {
		this.evidences = evidences;
	}
		
	public Boolean getAuditted() {
		return auditted;
	}
	public void setAuditted(Boolean auditted) {
		this.auditted = auditted;
	}
	
	@NotNull
	@OneToOne
	public User getEnteringUser() {
		return enteringUser;
	}
	public void setEnteringUser(User enteringUser) {
		this.enteringUser = enteringUser;
	}
	
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	public Date getEnteringDate() {
		return enteringDate;
	}
	public void setEnteringDate(Date enteringDate) {
		this.enteringDate = enteringDate;
	}
	
	@OneToOne(cascade = CascadeType.ALL)
	public RejectRecord getRejectRecord() {
		return rejectRecord;
	}
	public void setRejectRecord(RejectRecord rejectRecord) {
		this.rejectRecord = rejectRecord;
	}
	
	@Length(max = 2000)
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	@OneToOne
	public User getAuditUser() {
		return auditUser;
	}
	public void setAuditUser(User auditUser) {
		this.auditUser = auditUser;
	}
	@Temporal(TemporalType.TIMESTAMP)
	public Date getAuditDate() {
		return auditDate;
	}
	public void setAuditDate(Date auditDate) {
		this.auditDate = auditDate;
	}
	
	public boolean isHasSendMsg() {
		return hasSendMsg;
	}
	public void setHasSendMsg(boolean hasSendMsg) {
		this.hasSendMsg = hasSendMsg;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((auditted == null) ? 0 : auditted.hashCode());
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + ((enteringDate == null) ? 0 : enteringDate.hashCode());
		result = prime * result + ((remark == null) ? 0 : remark.hashCode());
		result = prime * result + ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		UndeterminedRoster other = (UndeterminedRoster) obj;
		if (auditted == null) {
			if (other.auditted != null)
				return false;
		} else if (!auditted.equals(other.auditted))
			return false;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (enteringDate == null) {
			if (other.enteringDate != null)
				return false;
		} else if (!enteringDate.equals(other.enteringDate))
			return false;
		if (remark == null) {
			if (other.remark != null)
				return false;
		} else if (!remark.equals(other.remark))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		return true;
	}
	
	
	@Transient
	public String getAudittedStr() {
		if(getAuditted() == null) {
			return "待审核";
		}else if(getAuditted()) {
			return "审核通过";
		}else {
			return "驳回";
		}
	}
	
	@Transient
	public String getSuppliersStr() {
		StringBuilder sb = new StringBuilder();
		for (ServiceSupplier supplier : getSuppliers()) {
			sb.append(supplier.getCnname());
			sb.append("、");
		}
		String str = "";
		if(sb.length() > 0) str = sb.substring(0, sb.length()-1);
		return str;
	}
	
}
